<?php

namespace app\admin\controller\business;

//引入进来基类控制器
use app\common\controller\Backend;

/**
 * 客户公海控制器
 * 涉及表：business、admin、business_receive
 */
class Privateseainfo extends Backend
{
  //设置关联查询
  // 如果不设置这个，则会出现id冲突
  protected $relationSearch = true;

  //当前模型
  protected $model = null;

  //当前无须登录方法
  protected $noNeedLogin = [];

  //无需鉴权的方法,但需要登录
  protected $noNeedRight = [];

  //构造函数
  public function __construct()
  {
    parent::__construct();

    //将控制器和模型关联
    $this->model = model('Business.Business');
    $this->SourceModel = model('Business.Source');
    $this->ReceiveModel = model('Business.Receive');
    $this->AdminModel = model('Business.Admin');
    $this->RegionModel = model('Business.Region');
    $this->VisitModel = model('Business.Visit');
  }

  // 列表
  public function index()
  {

    // 接收返回的id
    $ids = $this->request->param('ids', 0, 'trim');
    // 查询用户是否存在
    $row = $this->model->find($ids);

    // 赋值数据到模板中
    $this->assign('row', $row);
    // 模板渲染
    return $this->view->fetch();
  }

  // 回访记录
  public function visit($ids = NULL)
  {

    // 
    $this->model = $this->VisitModel;

    //将请求当中所有的参数去除html标签，去掉两边空白
    $this->request->filter(['strip_tags', 'trim']);

    // 判断是否有ajax
    if ($this->request->isAjax()) {
      //获取表格所提交过来筛选和排序的一些参数
      // 偏移量：$offset， 每页显示多少：$limit
      list($where, $sort, $order, $offset, $limit) = $this->buildparams();

      // 表格需要两个返回值参数 总条数，分页的数据
      // 总条数
      $total = $this->model
        ->with(['admin', 'business'])
        ->where('busid', $ids)
        ->where($where)
        ->order($sort, $order)
        ->count();

      // 每一页展示的数据
      $list = $this->model
        ->with(['admin', 'business'])
        ->where('busid', $ids)
        ->where($where)
        ->order($sort, $order)
        ->limit($offset, $limit)
        ->select();

      // 组装数据
      $result = [
        'total' => $total,
        'rows' => $list,
      ];

      // 返回json格式数据给ajax
      return json($result);
    }

    // 模板渲染
    return $this->view->fetch();
  }

  // 回访添加
  public function add($ids = NULL)
  {

    // 判断是否有post请求
    if ($this->request->isPost()) {
      $params = $this->request->post('row/a');
      $params['busid'] = $ids;
      $params['adminid'] = $this->auth->id;

      if ($params) {
        // 插入到模型中
        $result = $this->VisitModel->validate("Common/Business/Visit")->save($params);
        if ($result === false) {
          $this->error($this->VisitModel->getError());
          exit;
        }
        $this->success();
        exit;
      }
    }

    // 模板渲染
    return $this->view->fetch();
  }

  // 回访编辑
  public function edit($ids = null)
  {

    $row = $this->VisitModel->find($ids);
    if (!$row) {
      $this->error(__('未找到当前回访记录'));
      exit;
    }

    if ($this->request->isPost()) {
      $params = $this->request->post("row/a");

      if ($params) {
        $params['id'] = $ids;
        $params['busid'] = $row['busid'];
        $params['adminid'] = $row['adminid'];
        $result = $this->VisitModel->validate("Common/Business/Visit")->isUpdate(true)->save($params);

        if ($result === false) {
          $this->error($this->VisitModel->getError());
          exit;
        }

        $this->success();
        exit;
      }
    }

    $this->assign("row", $row);

    return $this->view->fetch();
  }

  // 回访删除
  public function del($ids = null)
  {
    $ids = !empty($ids) ?  explode(',', $ids) : [];
    $row = $this->VisitModel->column('id');

    foreach ($ids as $item) {
      if (!in_array($item, $row)) {
        $this->error(__('没有找到该回访记录'));
        exit;
      }
    }

    $result = $this->VisitModel->destroy($ids);

    if ($result === false) {
      $this->error($this->VisitModel->getError());
      exit;
    }

    $this->success();
    exit;
  }

  // 申请记录
  public function receive($ids = null)
  {
    //设置过滤方法
    $this->request->filter(['strip_tags']);

    if ($this->request->isAjax()) {
      if ($this->request->request('keyField')) {
        return $this->selectpage();
      }

      list($where, $sort, $order, $offset, $limit) = $this->buildparams();

      $total = $this->ReceiveModel
        ->with(['admin', 'business'])
        ->where($where)
        ->where('busid', $ids)
        ->order($sort, $order)
        ->limit($offset, $limit)
        ->count();  //查询总数

      $list = $this->ReceiveModel
        ->with(['admin', 'business'])
        ->where($where)
        ->where('busid', $ids)
        ->order($sort, $order)
        ->limit($offset, $limit)
        ->select();  //查询数据

      $result = array("total" => $total, "rows" => $list);

      return json($result);
    }

    return $this->view->fetch();
  }
}
